//
//  MageRelationShipTableViewCell.m
//  BGM
//
//  Created by liuqin on 2018/12/4.
//  Copyright © 2018 com.alirtc.demo. All rights reserved.
//

#import "MageRelationShipTableViewCell.h"
#import "Common.h"
#import "UIImageView+WebCache.h"
@interface shipClass : UIView
@property (nonatomic, strong)UIImageView *headImage;
@property (nonatomic, strong)UILabel *nameLabel;
@property (nonatomic, strong)UILabel *helpLabel;


@end
@implementation shipClass

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self != nil) {
        self.headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.width)];
        self.headImage.layer.masksToBounds = YES;
        self.headImage.layer.cornerRadius = frame.size.width/2;
        [self.headImage setImage:[UIImage imageNamed:@"headImage"]];
        [self addSubview:self.headImage];
        
        
        self.helpLabel = [[UILabel alloc]initWithFrame:CGRectMake(frame.size.width-15, 0, 15, 15)];
        self.helpLabel.text = @"转";
        self.helpLabel.layer.cornerRadius = 15/2;
        self.helpLabel.layer.masksToBounds = YES;
        self.helpLabel.backgroundColor = bgmyelloeColor;
        self.helpLabel.textColor = bgmmainColor;
        self.helpLabel.textAlignment = NSTextAlignmentCenter;
        self.helpLabel.font = [UIFont systemFontOfSize:11];
        [self addSubview:self.helpLabel];
        
        self.helpLabel.hidden = YES;
        
        self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, frame.size.width, frame.size.width, 20)];
        self.nameLabel.text = @"张培林";
        self.nameLabel.font = [UIFont systemFontOfSize:10];
        self.nameLabel.textColor = bgmgrayColor;
        self.nameLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.nameLabel];
       
    }
    return self;
}



@end




@interface MageRelationShipTableViewCell()

@property (nonatomic, strong)NSArray *shipArray;
@property (nonatomic, strong)UIScrollView *shipSc;


@end

@implementation MageRelationShipTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self != nil) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.shipSc = [[UIScrollView alloc]initWithFrame:CGRectMake(30, 0, SCREENW-60, 100)];
        self.shipSc.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.shipSc];
        
    }
    return self;
}

- (void)shipView:(NSArray *)array isPublicMsg:(BOOL)isPublicMsg form:(NSString *)from{
    
    for (int i = 0; i<self.shipSc.subviews.count; i++) {
        UIView *sub = self.shipSc.subviews[i];
        [sub removeFromSuperview];
    }
    if(![array isKindOfClass:[NSArray class]]){
        return;
    }
    
    if (isPublicMsg) {
        [self publicMsgInfo:array form:from];
    }else{
        [self userFriendInfo:array];
    }

}
- (void)userFriendInfo:(NSArray *)array{
    
    self.shipSc.frame = CGRectMake(30, 0, SCREENW-60, 100);
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 300, 30)];
    label.text = @"关系脉络";
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = bgmgrayColor;
    [self.shipSc addSubview:label];
    
    for (int i = 0; i<array.count; i++) {
        NSDictionary *dic = [array objectAtIndex:i];
        
        shipClass *sview = [[shipClass alloc]initWithFrame:CGRectMake(60*i, 40, 40, 60)];
        sview.tag = i;
        sview.nameLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"userName"]];
        NSString *str = [NSString stringWithFormat:@"%@",[dic objectForKey:@"userHead"]];
        [sview.headImage sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"headImage"]];
        [self.shipSc addSubview:sview];
        self.shipSc.contentSize  = CGSizeMake(60*i+60, 60);
        str = [NSString stringWithFormat:@"%@",[dic objectForKey:@"helps"]];
        if([str isEqualToString:@"helps"]){
            sview.helpLabel.hidden = NO;
        }else{
            sview.helpLabel.hidden = YES;
        }
        
        if (i < array.count-1) {
            UIImageView *ship = [[UIImageView alloc]initWithFrame:CGRectMake(sview.frame.origin.x+sview.frame.size.width+7, 55, 7, 9)];
            [ship setImage:[UIImage imageNamed:@"push_right"]];
            [self.shipSc addSubview:ship];
        }
                self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickView:)];
        [sview addGestureRecognizer:tap];
    }
}

- (void)publicMsgInfo:(NSArray *)array form:from{
    
    self.shipSc.frame = CGRectMake(30, 0, SCREENW-60, 160);
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 300, 30)];
    label1.font = [UIFont systemFontOfSize:13];
    label1.textColor = bgmsmallColor;
    [self.shipSc addSubview:label1];
    
    NSString *message = [NSString stringWithFormat:@"      本条求助信息来自第三方平台 %@",from];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:message];
    
    NSRange range = [message rangeOfString:from];
    [string addAttribute:NSForegroundColorAttributeName value:bgmyelloeColor range:range];
    label1.attributedText = string;
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 12, 15, 15)];
    [imageView setImage:[UIImage imageNamed:@"publicSnow"]];
    [self.shipSc addSubview:imageView];
    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 300, 30)];
    label2.text = @"你的这些朋友也帮助了他";
    label2.font = [UIFont systemFontOfSize:14];
    label2.textColor = bgmgrayColor;
    [self.shipSc addSubview:label2];
    
    for (int i = 0; i<array.count; i++) {
        NSDictionary *dic = [array objectAtIndex:i];
        shipClass *sview = [[shipClass alloc]initWithFrame:CGRectMake(60*i, 70, 40, 60)];
        sview.tag = i;
        sview.nameLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"usersName"]];
        NSString *str = [NSString stringWithFormat:@"%@",[dic objectForKey:@"usersHead"]];
        [sview.headImage sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"headImage"]];
        [self.shipSc addSubview:sview];
        self.shipSc.contentSize  = CGSizeMake(60*i+60, 60);

        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickView:)];
        [sview addGestureRecognizer:tap];
    }
}

- (void)clickView:(UITapGestureRecognizer *)gesture{
    UIView *v =gesture.view;
    int tag = (int)v.tag;
    [self.shipDelegate findFriendInfo:tag];
}



@end
